﻿using System;
using System.IO;
using System.Windows.Forms;

namespace SmartMotion.Core
{
    public class ProductMgr
    {
        #region 单例

        private static readonly Lazy<ProductMgr> Instance = new Lazy<ProductMgr>(() => new ProductMgr());

        private ProductMgr()
        {
        }

        public static ProductMgr Ins { get; } = Instance.Value;

        #endregion 单例

        public string FilePath { get; set; } = string.Empty;

        #region 产品管理

        public void New()
        {
            GlobalVars.Ins.Vars[GlobalVars.ProductPars].Clear();
        }

        public void Open()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "产品文件（*.product）| *.product";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() != DialogResult.OK) return;
            FilePath = openFileDialog.FileName;

            Open(FilePath);
        }

        public void Open(string file)
        {
            //if (File.Exists(file))
            {
                FilePath = file;
                GlobalVars.Ins.LoadDic(GlobalVars.ProductPars, FilePath, false);

            }
        }

        public void Save()
        {
            if (!File.Exists(FilePath))
            {
                SaveAs();
            }
            GlobalVars.Ins.SaveDic(GlobalVars.ProductPars, FilePath);
        }

        public void SaveAs()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "产品文件（*.product）| *.product";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog()==DialogResult.OK)
            {
                FilePath = sfd.FileName;
            }
        }

        #endregion 产品管理
    }
}